using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using HengTong.Model.Db.SupplyChain.Purchase;
using TiaoTaoHelper.Wtm.Models;
using TiaoTaoHelper.Wtm.Interfaces;


namespace HengTong.ViewModel.SupplyChain.SysPurchaseRequestBillVms
{
    public partial class SysPurchaseRequestBillListVm : BaseListVm<SysPurchaseRequestBillView, SysPurchaseRequestBillSearcher>
    {

        protected override IEnumerable<IGridColumn<SysPurchaseRequestBillView>> InitGridHeader()
        {
            return new List<GridColumn<SysPurchaseRequestBillView>>{
                this.MakeGridHeader(x => x.Applicant),
                this.MakeGridHeader(x => x.RequestDate),
                this.MakeGridHeader(x => x.NeedDate),
                this.MakeGridHeader(x => x.PurchaseType),
                this.MakeGridHeader(x => x.MaterialType),
                this.MakeGridHeader(x => x.UrgencyLevel),
                this.MakeGridHeader(x => x.UrgencyReason),
                this.MakeGridHeader(x => x.Amount),
                this.MakeGridHeader(x => x.Approver),
                this.MakeGridHeader(x => x.ApprovalTime),
                this.MakeGridHeader(x => x.GroupName),
                this.MakeGridHeader(x => x.OrgName),
                this.MakeGridHeader(x => x.BillCode),
                this.MakeGridHeader(x => x.BillStatus),
                this.MakeGridHeader(x => x.WorkflowStatus),
                this.MakeGridHeader(x => x.Remark),
                this.MakeGridHeader(x => x.CanApprove).SetHide().SetFormat((a, b) =>
                {
                    if(a.CanApprove) { return "true"; }
                    else { return "false"; }
                }),

                this.MakeGridHeaderAction(width: 200)
            };
        }
        public override void AfterDoSearcher()
        {
            if (Wtm.LoginUserInfo != null && typeof(IBill).IsAssignableFrom(ModelType))
            {
                List<SysWorkflow> ids = GetMyApproves();
                if (ids.Any())
                {
                    foreach (var item in EntityList)
                    {
                        var m = ids.Find(x => x.ModelId == item.GetID().ToString());
                        if (m != null && item.WorkflowStatus == WorkflowStatus.审核中)
                        {
                            item.CanApprove = true;
                        }
                    }
                }
            }
            base.AfterDoSearcher();
        }


        public override IOrderedQueryable<SysPurchaseRequestBillView> GetSearchQuery()
        {
            var query = DC.Set<SysPurchaseRequestBill>()
                .CheckContain(Searcher.Applicant, x=>x.Applicant)
                .CheckBetween(Searcher.RequestDate?.GetStartTime(), Searcher.RequestDate?.GetEndTime(), x => x.RequestDate, includeMax: false)
                .CheckBetween(Searcher.NeedDate?.GetStartTime(), Searcher.NeedDate?.GetEndTime(), x => x.NeedDate, includeMax: false)
                .CheckEqual(Searcher.PurchaseType, x=>x.PurchaseType)
                .CheckEqual(Searcher.MaterialType, x=>x.MaterialType)
                .CheckEqual(Searcher.UrgencyLevel, x=>x.UrgencyLevel)
                .CheckContain(Searcher.Approver, x=>x.Approver)
                .CheckBetween(Searcher.ApprovalTime?.GetStartTime(), Searcher.ApprovalTime?.GetEndTime(), x => x.ApprovalTime, includeMax: false)
                .CheckEqual(Searcher.GroupId, x=>x.GroupId)
                .CheckEqual(Searcher.OrgId, x=>x.OrgId)
                .CheckContain(Searcher.BillCode, x=>x.BillCode)
                .CheckEqual(Searcher.BillStatus, x=>x.BillStatus)
                .CheckEqual(Searcher.WorkflowStatus, x=>x.WorkflowStatus)
                .CheckContain(Searcher.Remark, x=>x.Remark)
                .Select(x => new SysPurchaseRequestBillView
                {
				    ID = x.ID,
                    Applicant = x.Applicant,
                    RequestDate = x.RequestDate,
                    NeedDate = x.NeedDate,
                    PurchaseType = x.PurchaseType,
                    MaterialType = x.MaterialType,
                    UrgencyLevel = x.UrgencyLevel,
                    UrgencyReason = x.UrgencyReason,
                    Amount = x.Amount,
                    Approver = x.Approver,
                    ApprovalTime = x.ApprovalTime,
                    GroupName = x.Group.GroupName,
                    OrgName = x.Org.Name,
                    BillCode = x.BillCode,
                    BillStatus = x.BillStatus,
                    WorkflowStatus = x.WorkflowStatus,
                    Remark = x.Remark,
                })
                .OrderBy(x => x.ID);
            return query;
        }
    }

    public class SysPurchaseRequestBillView : SysPurchaseRequestBill{
        [Display(Name = "_Admin.GroupName")]
        public String GroupName { get; set; }
        [Display(Name = "组织名称")]
        public String OrgName { get; set; }
        public bool CanApprove { get; set; }


    }
}
